# Version: 2.5.1 (stable environment)
FROM registry.baidubce.com/paddlepaddle/paddle:2.5.1


# Set TimeZone - Asia/Shanghai
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo 'Asia/Shanghai' > /etc/timezone


# Set file cache workspace
ARG cache_images_dir=/PaddleOCR/cache/images
ARG load_models_dir=/PaddleOCR/inference

WORKDIR /PaddleOCR
RUN mkdir -p /${cache_images_dir} \
    && mkdir -p ${load_models_dir}

# Clone code from gitee.com(Or download by manual)
COPY . /PaddleOCR

# PaddleOCR base on Python3.7(If use network - outside)
RUN pip3 install --upgrade pip \
    && pip3 install -r requirements.txt


# [1] Detect model(Chinese and English)
ARG det_model_ch_link=https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar
ARG det_model_ch_file=ch_PP-OCRv3_det_infer.tar

ARG det_model_en_link=https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar
ARG det_model_en_file=en_PP-OCRv3_det_infer.tar

#  Multi - lang
# ARG det_model_multi_link=https://paddleocr.bj.bcebos.com/PP-OCRv3/multilingual/Multilingual_PP-OCRv3_det_infer.tar
# ARG det_model_multi_file=Multilingual_PP-OCRv3_det_infer.tar

# [2] Direction classify modle
ARG classify_model_link=https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
ARG classify_model_file=ch_ppocr_mobile_v2.0_cls_infer.tar

# [3] Recognition model(Chinese and English)
ARG rec_model_ch_link=https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar
ARG rec_model_ch_file=ch_PP-OCRv3_rec_infer.tar

ARG rec_model_en_link=https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_infer.tar
ARG rec_model_en_file=en_PP-OCRv3_rec_infer.tar

# [4] PP-structure
# Image orientation model
ARG image_orientation_model_link=https://paddleclas.bj.bcebos.com/models/PULC/inference/text_image_orientation_infer.tar
ARG image_orientation_model_file=text_image_orientation_infer.tar

# Layout abalysis model(Tips: model version changed, rename "model.*" --> "inference.*" file)
ARG layout_model_link=https://paddleocr.bj.bcebos.com/ppstructure/models/layout/picodet_lcnet_x1_0_fgd_layout_cdla_infer.tar
ARG layout_model_file=picodet_lcnet_x1_0_fgd_layout_cdla_infer.tar
ARG layout_model_dir=picodet_lcnet_x1_0_fgd_layout_cdla_infer

# chinese table recognize model
ARG table_model_link=https://paddleocr.bj.bcebos.com/ppstructure/models/slanet/ch_ppstructure_mobile_v2.0_SLANet_infer.tar
ARG table_model_file=ch_ppstructure_mobile_v2.0_SLANet_infer.tar

#ARG ser_model_link=https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/ser_vi_layoutxlm_xfund_infer.tar
#ARG ser_model_file=ser_vi_layoutxlm_xfund_infer.tar

#ARG re_model_link=https://paddleocr.bj.bcebos.com/ppstructure/models/vi_layoutxlm/re_vi_layoutxlm_xfund_infer.tar
#ARG re_model_file=re_vi_layoutxlm_xfund_infer.tar


# Download models
# [1] Download OCR text detect model.
RUN wget --no-check-certificate ${det_model_ch_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${det_model_ch_file} -C /PaddleOCR/inference/

RUN wget --no-check-certificate ${det_model_en_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${det_model_en_file} -C /PaddleOCR/inference/

#RUN wget --no-check-certificate ${det_model_multi_link} -P /PaddleOCR/inference/ \
#    && tar xf /PaddleOCR/inference/${det_model_multi_file} -C /PaddleOCR/inference/

# [2] Download direction classifier.
RUN wget --no-check-certificate ${classify_model_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${classify_model_file} -C /PaddleOCR/inference/

# [3] Download OCR text infer model.
RUN wget --no-check-certificate ${rec_model_ch_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${rec_model_ch_file} -C /PaddleOCR/inference/

RUN wget --no-check-certificate ${rec_model_en_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${rec_model_en_file} -C /PaddleOCR/inference/

# [4] Download ppstructure model
RUN wget --no-check-certificate ${image_orientation_model_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${image_orientation_model_file} -C /PaddleOCR/inference/

# Layout model - process(Version changed(Tips: rename "model.*" --> "inference.*" file))
RUN wget --no-check-certificate ${layout_model_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${layout_model_file} -C /PaddleOCR/inference/ \
    && mv /PaddleOCR/inference/${layout_model_dir}/model.pdiparams /PaddleOCR/inference/${layout_model_dir}/inference.pdiparams \
    && mv /PaddleOCR/inference/${layout_model_dir}/model.pdiparams.info /PaddleOCR/inference/${layout_model_dir}/inference.pdiparams.info \
    && mv /PaddleOCR/inference/${layout_model_dir}/model.pdmodel /PaddleOCR/inference/${layout_model_dir}/inference.pdmodel


RUN wget --no-check-certificate ${table_model_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${table_model_file} -C /PaddleOCR/inference/

#RUN wget --no-check-certificate ${ser_model_link} -P /PaddleOCR/inference/ \
#    && tar xf /PaddleOCR/inference/${ser_model_file} -C /PaddleOCR/inference/

#RUN wget --no-check-certificate ${re_model_link} -P /PaddleOCR/inference/ \
#    && tar xf /PaddleOCR/inference/${re_model_file} -C /PaddleOCR/inference/


# Remove invalid *.tar
RUN rm -f /PaddleOCR/inference/*.tar


# Export Ports
ARG port=5055
EXPOSE ${port}

# Set ENV-VARS
ENV FILE_CACHE_PATH=${cache_images_dir}
ENV port=${port}

# Service
RUN chmod +x /PaddleOCR/start_service.sh
CMD ["/bin/bash", "-c", "/PaddleOCR/start_service.sh"]
